package edu.neu.cloud_factory.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import edu.neu.cloud_factory.entity.po.ProductType;
import edu.neu.cloud_factory.dao.ProductTypeMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.neu.cloud_factory.entity.vo.ProductTypeVo;
import edu.neu.cloud_factory.entity.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品类型 服务实现类
 * </p>
 *
 * @author halozhy
 * @since 2021-07-15
 */
@Service
public class ProductTypeService extends ServiceImpl<ProductTypeMapper, ProductType> implements IService<ProductType> {
    @Autowired
    ProductTypeMapper productTypeMapper;

    public JSONObject listProdType(Integer page, Integer limit, String id, String name) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        if (id != null && !id.isEmpty()) {
            queryWrapper.like("pt_id", id);
        }
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("pt_name", name);
        }

        List<ProductType> productTypeList = productTypeMapper.listPage((page - 1) * limit, limit, queryWrapper);
        List<ProductTypeVo> productTypeVoList = new ArrayList<>();
        productTypeList.forEach(productType -> {
            ProductTypeVo productTypeVo = new ProductTypeVo();
            productTypeVo.setId(productType.getPtId());
            productTypeVo.setName(productType.getPtName());
            productTypeVoList.add(productTypeVo);
        });
        JSONObject result = new JSONObject();
        result.put("data", productTypeVoList);
        result.put("count", productTypeMapper.selectCount(queryWrapper));
        return result;
    }
}
